﻿using System.Net.Mime;
using System.IO;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Web;
using UMC.Wiki.Entities;
using UMC.Net;

namespace UMC.Wiki.Activities
{
    class SubjectPublishActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var Key = this.AsyncDialog("Key", g =>
            {
                this.Prompt("Key");
            });
            var type = this.AsyncDialog("type", "Sub");

            var id = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", "auto"));

            if (id.HasValue && request.IsMaster == false)
            {
                ulong projectId = 0;
                switch (type)
                {
                    case "Sub":
                        projectId = DataFactory.Instance().Subject(id.Value).project_id.Value;
                        break;
                    case "Item":
                        projectId = DataFactory.Instance().ProjectItem(id.Value).project_id.Value;
                        break;
                    case "Project":
                        projectId = id.Value;
                        break;
                }
                var member = DataFactory.Instance().ProjectMember(projectId, this.Context.Token.UserId.Value);

                if (member != null)
                {
                    switch (member.AuthType)
                    {
                        case WebAuthType.Admin:
                        case WebAuthType.User:
                            break;
                        default:
                            response.Redirect(new WebMeta().Put("msg", "权限不足,不能静态发布"));
                            return;
                    }

                }
                else
                {
                    response.Redirect(new WebMeta().Put("msg", "权限不足,不能静态发布"));
                    return;
                }
            }
            else if (request.IsMaster == false)
            {
                response.Redirect(new WebMeta().Put("msg", "权限不足,不能静态发布"));
                return;
            }
            var staticFile = UMC.Data.Reflection.ConfigPath(String.Format("Static/{0}/index.html", Key.Trim('/')));
            if (String.Equals(Key, "index"))
            {
                staticFile = UMC.Data.Reflection.ConfigPath("Static/index.html");
            }
            var staticTpl = UMC.Data.Reflection.ConfigPath("Static/404.OK.html");
            if (!System.IO.File.Exists(staticTpl))
            {
                return;
            }

            var title = String.Format("{0} · 知识录", this.AsyncDialog("title", "title"));
            var keyword = this.AsyncDialog("keyword", "keyword");
            var desc = this.AsyncDialog("desc", "desc");
            var html = this.AsyncDialog("html", "html");
            if (html.Length < 200)
            {
                return;

            }

            desc = (desc.Length > 150 ? desc.Substring(0, 150) : desc).Replace('"', '’');
            keyword = (keyword.Length > 100 ? keyword.Substring(0, 100) : keyword).Replace('"', '’');
            using (System.IO.Stream stream = System.IO.File.OpenRead(staticTpl))
            {
                var ms = new System.IO.MemoryStream();
                var writer = new System.IO.StreamWriter(ms);
                HtmlPublish.Release(html, title, keyword, desc, new System.IO.StreamReader(stream).ReadToEnd(), writer);
                writer.Flush();
                if (ms.Length > 0)
                {
                    ms.Position = 0;
                    UMC.Data.Utility.Copy(ms, staticFile);
                    ms.Close();
                    ms.Dispose();
                    if (id.HasValue)
                    {
                        switch (type)
                        {
                            case "Sub":
                                DataFactory.Instance().Put(new Subject { PublishTime = UMC.Data.Utility.TimeSpan(), Id = id.Value });
                                break;
                            case "Item":
                                DataFactory.Instance().Put(new ProjectItem { PublishTime = UMC.Data.Utility.TimeSpan(), Id = id.Value });
                                break;
                            case "Project":
                                DataFactory.Instance().Put(new Project { PublishTime = UMC.Data.Utility.TimeSpan(), Id = id.Value });
                                break;
                        }
                    }
                }
            }
            response.Redirect(new WebMeta().Put("src", new Uri(request.Url, String.Format("/{0}", Key.TrimStart('/')))));

        }
    }
}